var table;
$(function() {
	table = dataTable.init('table', {
		url : RS.webPath + '/admin/user/listUserByOrgan',
		toolbar : '#toolbar',
		cols : [ [ {
			checkbox : true,
			fixed : true
		}, {
			field : 'userName',
			title : '用户名',
			fixed : 'left'
		}, {
			field : 'userStatus',
			title : '状态',
			fixed : 'left',
			templet : fus
		} ] ],
		searchData : {
			userName : $('#userName').val(),
			status : $('#status').val(),
			organId : organId
		}
	});

	var form = layui.form;
	/* 监听提交 */
	form.on('submit(query-form)', function(data) {
		query();
		return false;
	});
	form.render();

});

function query() {
	table.search({
		userName : $('#userName').val(),
		status : $('#status').val(),
		organId : organId
	});
}
function add() {
	window.location.href = RS.webPath
			+ '/admin/user/edit?method=INSERT&organId=' + organId;
}
function del() {
	var selections = table.getAllSelected();
	if (selections.length < 1) {
		parent.layer.alert("您未选择任何一条记录");
		return;
	}
	parent.layer.confirm("是否要删除选中的项目？", {
		icon : 3,
		title : '提示',
	}, function(index) {
		var ids = [];
		for (var i = 0; i < selections.length; i++) {
			ids[i] = selections[i].userId
		}
		$.ajax({
			url : RS.webPath + '/admin/user/del',
			type : 'post',
			dataType : 'json',
			data : '__paramSet__=' + JSON.stringify(ids),
			success : function(ret) {
				if (ret.status == 0) {
					parent.layer.alert('删除失败', function(index) {
						parent.layer.close(index);
						query();
					});
				} else {
					parent.layer.alert('删除成功', function(index) {
						parent.layer.close(index);
						query();
					});
				}
			}
		});
	});
}
function edit() {
	var selections = table.getAllSelected();
	if (selections.length > 1) {
		parent.layer.alert("只能选择一个项目进行编辑");
		return;
	}
	var id = selections[0].userId;
	window.location.href = RS.webPath + '/admin/user/edit?method=UPDATE&id='
			+ id + "&organId=" + organId;
}
function settingRole() {
	var selections = table.getAllSelected();
	if (selections.length > 1) {
		parent.layer.alert("只能选择一个项目进行编辑");
		return;
	}
	var id = selections[0].userId;
	window.location.href = RS.webPath + '/admin/user/settingRole?id=' + id
			+ "&organId=" + organId;
}
function settingOrgan() {
	var selections = table.getAllSelected();
	if (selections.length > 1) {
		parent.layer.alert("只能选择一个项目进行编辑");
		return;
	}
	if (selections.length < 1) {
		parent.layer.alert("请选择一个项目进行编辑");
		return;
	}

	var id = selections[0].userId;
	parent.layer.open({
		type : 2,
		shadeClose : true,
		shade : 0.8,
		area : [ '380px', '90%' ],
		content : RS.webPath + '/admin/user/settingOrgan?id=' + id
				+ "&organId=" + organId // iframe的url
	});
}

function fus(value, row, index) {
	value = value.userStatus;
	if (value == 1) {
		return "正常";
	} else if (value == 0) {
		return "注销";
	} else if (value == -1) {
		return "锁定";
	} else {
		return "未知";
	}
}